package com.java110.mall.goods.cmd.shopAppraise;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartEventDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderCartEventInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.goods.IShopAppraiseInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.shop.ShopAppraisePo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderCartEventPo;
import com.java110.utils.constant.StatusConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "/shopAppraise/saveShopAppraise")
public class SaveShopAppraiseCmd extends Cmd {


    @Autowired
    private IShopAppraiseInnerServiceSMO shopAppraiseInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartEventInnerServiceSMO storeOrderCartEventInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "context", "请求报文中未包含context");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        String userId = CmdContextUtils.getUserId(context);
        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        Assert.listOnlyOne(userDtos, "用户未登录");

        ShopAppraisePo shopAppraisePo = BeanConvertUtil.covertBean(reqJson, ShopAppraisePo.class);
        shopAppraisePo.setAppraiseUserId(userId);
        shopAppraisePo.setAppraiseUserName(userDtos.get(0).getName());
        shopAppraisePo.setAppraiseId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_appraiseId));
        int flag = shopAppraiseInnerServiceSMOImpl.saveShopAppraise(shopAppraisePo);

        if (flag < 1) {
            throw new IllegalArgumentException("保存失败");

        }

        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setCartId(shopAppraisePo.getCartId());
        storeOrderCartDto.setShopId(shopAppraisePo.getShopId());
        storeOrderCartDto.setState(StoreOrderCartDto.STATE_WAIT_REPAIR);
        List<StoreOrderCartDto> storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);

        if (ListUtil.isNull(storeOrderCartDtos)) {
            return;
        }

        //加入事件

        StoreOrderCartEventPo storeOrderCartEventPo = new StoreOrderCartEventPo();
        storeOrderCartEventPo.setCartId(shopAppraisePo.getCartId());
        storeOrderCartEventPo.setOrderId(storeOrderCartDtos.get(0).getOrderId());
        storeOrderCartEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
        storeOrderCartEventPo.setEventObjType("U");
        storeOrderCartEventPo.setEventObjId(shopAppraisePo.getAppraiseUserId());
        storeOrderCartEventPo.setShopId(shopAppraisePo.getShopId());
        storeOrderCartEventPo.setEventMsg(shopAppraisePo.getContext());
        storeOrderCartEventPo.setEventType(StoreOrderCartEventDto.EVENT_TYPE_COMMON);

        flag = storeOrderCartEventInnerServiceSMOImpl.saveStoreOrderCartEvent(storeOrderCartEventPo);

        if (flag < 1) {
            throw new IllegalArgumentException("保存购物车事件失败");
        }

        StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
        storeOrderCartPo.setShopId(storeOrderCartDtos.get(0).getShopId());
        storeOrderCartPo.setState(StoreOrderCartDto.STATE_FINISH);
        storeOrderCartPo.setStatusCd(StatusConstant.STATUS_CD_VALID);
        storeOrderCartPo.setCartId(storeOrderCartDtos.get(0).getCartId());
        flag = storeOrderCartInnerServiceSMOImpl.updateStoreOrderCart(storeOrderCartPo);

        if (flag < 1) {
            throw new CmdException("保存失败");
        }
        context.setResponseEntity(ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功"));
    }
}
